/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgEditBill.java
 *
 * Created on Aug 18, 2012, 3:03:21 PM
 */

package Model.dialog;

import DataBase.DBManagerment;
import Processing.dbclass.Bill;
import java.sql.Connection;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class dlgAddBill extends javax.swing.JDialog {

    /** Creates new form dlgEditBill */
    public dlgAddBill(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * Đối tượng của class DBManagerment
     */
    private DBManagerment db = new DBManagerment();

    /**
     * Một Connection
     */
    private Connection con = db.ConnectServer();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlBillingAdd = new javax.swing.JPanel();
        jLabel18 = new javax.swing.JLabel();
        txtBillAddPatientID = new javax.swing.JTextField();
        btnBillingAction = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Bill");
        setResizable(false);

        pnlBillingAdd.setPreferredSize(new java.awt.Dimension(219, 95));

        jLabel18.setText("Patient's ID:");

        txtBillAddPatientID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBillAddPatientIDActionPerformed(evt);
            }
        });

        btnBillingAction.setText("Add");
        btnBillingAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBillingActionActionPerformed(evt);
            }
        });

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBillingAddLayout = new javax.swing.GroupLayout(pnlBillingAdd);
        pnlBillingAdd.setLayout(pnlBillingAddLayout);
        pnlBillingAddLayout.setHorizontalGroup(
            pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBillingAddLayout.createSequentialGroup()
                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBillingAddLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel18)
                        .addGap(18, 18, 18)
                        .addComponent(txtBillAddPatientID, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlBillingAddLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(btnBillingAction, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        pnlBillingAddLayout.setVerticalGroup(
            pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBillingAddLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(txtBillAddPatientID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlBillingAddLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBillingAction)
                    .addComponent(jButton1))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        getContentPane().add(pnlBillingAdd, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

    private void btnBillingActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBillingActionActionPerformed
        // TODO add your handling code here:
        if(db.searchPatient(con, 1, txtBillAddPatientID.getText()).getRowCount() == 0){
            // Kiểm tra xem có Patient này ko, dùng method search Patient theo PatientID
            // trả về PikaTableModel, rồi xem PikatableModel này có số row == 0 hay ko?
            // Nếu số Row == 0 tức là ko có Patient với PatientID này

            JOptionPane.showMessageDialog(this, "This PatientID haven't existed!");
        }else{
            // Trong add bill này CollectorID có thể để trống nên ko cần kiểm tra

            // Nếu toàn bộ quá trình kiểm tra ko sai thì bắt đầu add vào

            // Tạo mới một Object Bill để chứa thông tin            

            bil.setPatientID(Integer.parseInt(txtBillAddPatientID.getText()));

            bil.setAmount(0);

            bil.setPaid(false);

            this.setVisible(false);

            // DateMade và DatePaid ko phải làm vì tớ đã sửa lại method add trong
            // DBManager, sẽ lấy ngày hiện tại
            // Report có DateMade tương tự cũng được tớ sủa method rồi
        }
}//GEN-LAST:event_btnBillingActionActionPerformed

    private void txtBillAddPatientIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBillAddPatientIDActionPerformed
        // TODO add your handling code here:
        btnBillingAction.doClick();
    }//GEN-LAST:event_txtBillAddPatientIDActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgAddBill dialog = new dlgAddBill(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * Biến chứa dữ liệu của bill
     */
    private Bill bil = new Bill();;

    /**
     * Method trả về bill để chương trình xử lý
     * @return Bill
     */
    public Bill getBill() {
        return bil;
    }

    /**
     * Method gán thông tin cho bill
     * @param bil
     */
    public void setBil(Bill bil) {
        this.bil = bil;

        txtBillAddPatientID.setText(""+bil.getPatientID());
    }

    /**
     * Method trả vể nut add
     * @return nut add
     */
    public JButton getBtnBillingAction() {
        return btnBillingAction;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBillingAction;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JPanel pnlBillingAdd;
    private javax.swing.JTextField txtBillAddPatientID;
    // End of variables declaration//GEN-END:variables

}
